// pages/restaurant/restaurant.js

const config = require('../../config/config.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: 0,
    collectNum: 0,
    openTime: '',
    endTime: '',
    latitude: 0,
    longitude: 0,
    location: '',
    logoUrl: '',
    notice: '',
    shopName: '',
    shopStyle: '',
    shopTel: '',
    isSelfShop: false,
    foods: [],
    isCollect: false,
    showSharePanel: false,
    selfFoodsCount: 0,
    maxFoosCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let shop_id = options.shop_id
    // 扫码进入页面
    if (!shop_id && options.scene) {
      let scene = decodeURIComponent(options.scene)
      shop_id = parseInt(scene)
    }
    let that = this
    wx.request({
      url: `${config.requestBaseUrl}/Shop/getShopInfo`,
      method: 'POST',
      data: {
        shop_id: shop_id,
        openid: wx.getStorageSync('openid')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        // console.log(res)
        if (res.data.code === 1) {
          that.setData({
            shopId: shop_id,
            isSelfShop: wx.getStorageSync('openid') == res.data.openid,
            collectNum: res.data.collect_num,
            openTime: res.data.open_time,
            endTime: res.data.end_time,
            latitude: res.data.latitude,
            longitude: res.data.longitude,
            location: res.data.location,
            logoUrl: res.data.logo_url,
            notice: res.data.notice,
            shopName: res.data.shop_name,
            shopStyle: res.data.shop_style,
            shopTel: res.data.tel,
            isCollect: res.data.is_collect == 1 ? true : false,
            maxFoosCount: res.data.max_foods_count,
          })
          that.getFoodsCount()
        } else {
          console.log("没有找到商户信息")
        }
      }
    })

    wx.request({
      url: `${config.requestBaseUrl}/Dishes/getAllFoodsInShop`,
      method: 'POST',
      data: {
        shop_id: shop_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        let data = res.data
        // 查看是否为自己菜品的字段
        for (let i = 0, len = data.length; i < len; i++) {
          data[i].is_self = wx.getStorageSync('openid') == data[i].openid
          // data[i].star_num = Math.round(data[i].star_score)
          data[i].star_shine = "./../../imgs/star_shine.png"
          data[i].star = "./../../imgs/star.png"
        }
        that.setData({
          foods: data
        })
        // console.log(that.data.foods)
      }
    })

  },

  // 获取当前商家菜品数量
  getFoodsCount: function() {
    let that = this
    if (this.data.isSelfShop) {
      wx.request({
        url: `${config.requestBaseUrl}/Dishes/getFoodsCountInShop`,
        method: 'POST',
        data: {
          shop_id: this.data.shopId,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          // console.log(res)
          that.setData({
            selfFoodsCount: res.data
          })
        }
      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.shopName,
      desc: this.data.notice,
      path: `pages/restaurant/restaurant?shop_id=${this.data.shopId}`
    }
  },

  clickAddFood: function () {
    
    if (this.data.selfFoodsCount >= this.data.maxFoosCount) {
      wx.showModal({
        content: `当前商家最大上传菜品数量为${this.data.maxFoosCount}，如想继续添加，请联系客服`,
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          }
        }
      })
      return
    } else {
      wx.navigateTo({
        url: `../foodform/foodform?shop_id=${this.data.shopId}&food_id=0&latitude=${this.data.latitude}&longitude=${this.data.longitude}`
      })
    }
  },

  openShopLocation: function () {
    // console.log(this.data.latitude)
    // console.log(typeof(this.data.latitude))
    wx.openLocation({
      latitude: Number(this.data.latitude),
      longitude: Number(this.data.longitude)
    })
  },

  makeShopPhoneCall: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.shopTel
    })
  },

  editShopInfo: function () {
    wx.navigateTo({
      url: `../shopform/shopform?shop_id=${this.data.shopId}&auth_code=''`,
    })
  },

  clickDeleteFood: function (event) {
    let foodId = event.currentTarget.dataset.id
    let foodName = event.currentTarget.dataset.name
    let that = this
    wx.showModal({
      title: '删除菜品',
      content: `确定删除当前选中菜品:${foodName}`,
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          that.deleteFood(foodId)
        }
      }
    })
  },

  deleteFood: function (foodId) {
    let that = this
    wx.request({
      url: `${config.requestBaseUrl}/Dishes/deleteFood`,
      method: 'POST',
      data: {
        food_id: foodId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        if (res.data.code == 1) {
          that.onLoad({
            shop_id: that.data.shopId
          })
        }
      }
    })

  },

  clickCollectShop: function() {
    let that = this
    wx.request({
      url: `${config.requestBaseUrl}/Collect/CollectShop`,
      method: 'POST',
      data: {
        shop_id: that.data.shopId,
        openid: wx.getStorageSync('openid')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        if (res.data.code != 0) {
          that.setData({
            isCollect: res.data.code === 1 ? true : false
          })
        }
      }
    })
  },

  clickShareBtn: function () {
    this.setData({
      showSharePanel: !this.data.showSharePanel
    })
  },

  generateShopShareImg: function() {
    let data = {
      shopId: this.data.shopId,
      shopName: this.data.shopName,
      shopTel: this.data.shopTel,
      logoUrl: this.data.logoUrl,
      shopStyle: this.data.shopStyle,
      location: this.data.location,
      openTime: this.data.openTime,
      endTime: this.data.endTime,
      notice: this.data.notice,
    }
    let dataJson = JSON.stringify(data)
    wx.redirectTo({
      url: `../shopcanvas/shopcanvas?data=${dataJson}`,
    })
  }


})